package com.kfm.validationdemo.controller;

import com.kfm.validationdemo.model.User;
import com.kfm.validationdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
public class UserController {

    // @Autowired @Resource 两者的区别
    @Autowired
    private UserService userService;

    @RequestMapping("/insert")
    // @Response
    // @RequestBody 请求体中的数据封装成一个对象
    // @Valid 两者的区别
    public String insert(@RequestBody User user) {
        // List<ObjectError> allErrors = bindingResult.getAllErrors();
        // if (allErrors != null) {
        //     StringBuilder message = new StringBuilder();
        //     for (ObjectError oe : allErrors) {
        //         String msg = oe.getDefaultMessage();
        //         message.append(msg).append(",");
        //     }
        //     return message.substring(0, message.length() - 1);
        // }
        userService.login();
        return "success";
    }

    @RequestMapping("/test")
    public String test() {
        String str = null;
        System.out.println(str.length());
        return "test";
    }
}
